//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.reminder;

import java.util.*;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.persistence.SQLList;

// Referenced classes of package org.jpgolay.me.reminder:
//            Reminder
public class ReminderCRUD {

    public ReminderCRUD() {
        init();
    }

    private void init() {
        sqlList = new SQLList();
        crud = new ObjectCRUD();
    }

    public Object[][] getDataList(String sql) {
        if (sqlList.search(sql)) {
            Object data[][] = sqlList.getRowsData();
            return data;
        } else {
            return null;
        }
    }

    public Integer insertReminderRow(Reminder reminder) {
        Integer pk = createReminder();
        if (pk != null) {
            reminder.setId(pk);
            updateReminderRow(reminder);
        }
        return pk;
    }

    public boolean updateReminderRow(Reminder reminder) {
        Reminder lastReminder = readReminder(reminder.getId());
        reminder.setUpdateLastTime(lastReminder.getUpdateLastTime());
        return updateReminder(reminder);
    }

    public Object[][] getDataReminder(String id) {
        Object data[][] = (Object[][]) null;
        Collection list = readAllReminder(id);
        if (list != null) {
            HashSet ref = new HashSet(list);
            Iterator it = ref.iterator();
            data = new Object[ref.size() + 30][4];
            int line = 0;
            Reminder reminder = null;
            while (it.hasNext()) {
                reminder = (Reminder) it.next();
                data[line][0] = reminder.getId();
                data[line][1] = reminder.getDate();
                data[line][2] = reminder.getNotes();
                if (reminder.getStatus().intValue() == 0) {
                    data[line][3] = new Boolean(false);
                } else {
                    data[line][3] = new Boolean(true);
                }
                line++;
            }
            for (int i = line; i < ref.size() + 30; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = new Boolean(false);
            }

        } else {
            data = new Object[30][4];
            for (int i = 0; i < 30; i++) {
                data[i][0] = null;
                data[i][1] = null;
                data[i][2] = null;
                data[i][3] = new Boolean(false);
            }

        }
        return data;
    }

    private Reminder readReminder(Integer id) {
        return (Reminder) crud.read(id, 20);
    }

    private Collection readAllReminder(String idPiece) {
        return crud.readAll(idPiece, 20);
    }

    private boolean updateReminder(Reminder data) {
        return crud.update(data, 20);
    }

    private Integer createReminder() {
        return (Integer) crud.create(20, null);
    }
    private SQLList sqlList;
    private ObjectCRUD crud;
}
